package test;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import main.DataConnection;

import org.junit.Assert;
import org.junit.Test;

import regression.AccidentRegression;
import regression.CarRegression;
import regression.EmissionRegression;
import regression.IRegression;
import regression.ProblemRegression;

/**
 * Test suite for {@code *Regression} classes. Tests regression output, data retrieval and file saving properties.
 * 
 * @author Dawid Nejman
 * @version 1.1
 */
public class RegressionTester {

	@Test
	public void fileTest() {
		IRegression carRegression = CarRegression.getInstance(DataConnection.getCarsData());
		IRegression accidentRegression = AccidentRegression.getInstance(DataConnection.getAccidentData());
		IRegression emissionRegression = EmissionRegression.getInstance(DataConnection.getEmissionData());
		Calendar cal = Calendar.getInstance();
		Random random = new Random();
		String[] filenames = new String[3];

		for (int i = 0; i < filenames.length; i++)
			filenames[i] = Integer.toString(Math.abs(random.nextInt()));

		// Car number
		for (int i = 0; i < 12; i++) {
			cal.set(2012, i, 1);
			Date date = cal.getTime();
			System.out
					.println("Date: " + cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + " Estimated cars: " + carRegression.getNumberFor(date));
		}

		carRegression.saveDataToFile(filenames[0]);

		// Accident number
		for (int i = 0; i < 12; i++) {
			cal.set(2011 + i, 1, 1);
			Date date = cal.getTime();
			System.out.println("Date: " + cal.get(Calendar.YEAR) + " Estimated accidents: " + accidentRegression.getNumberFor(date));
		}
		accidentRegression.saveDataToFile(filenames[1]);

		// Emission size
		for (int i = 0; i < 12; i++) {
			cal.set(2011 + i, 1, 1);
			Date date = cal.getTime();
			System.out.println("Date: " + cal.get(Calendar.YEAR) + " Estimated emmision: " + emissionRegression.getNumberFor(date));
		}
		emissionRegression.saveDataToFile(filenames[2]);

		for (String fileName : filenames) {
			File file = new File(fileName + "1.txt");
			Assert.assertTrue("File does not exists", file.exists());
			file.delete();
		}
	}
	
	@Test
	public void carRegressionTest() throws Throwable{
		ProblemRegression pre = new ProblemRegression("Emission");
		ProblemRegression pra = new ProblemRegression("Accidents", "Fatal Accidents");
		
		IRegression emissionRegression = EmissionRegression.getInstance(DataConnection.getEmissionData());
		IRegression accidentRegression = AccidentRegression.getInstance(DataConnection.getAccidentData());
		
		Calendar cal = Calendar.getInstance();
		cal.set(2040,1,1);
		
		double e1 = pre.getNumberFor(cal.getTime());
		double e2 = emissionRegression.getNumberFor(cal.getTime());
		double a1 = pra.getNumberFor(cal.getTime());
		double a2 = accidentRegression.getNumberFor(cal.getTime());
		
		System.out.println("Emission Regression\nnormal:" + e2 + "\ncar:" + e1 + "\nAccident Regression\nnormal:" + a2 + "\ncar:" + a1);
	}
}
